package cn.yuan.user.controller;

import cn.yuan.basic.Exception.BusinessException;
import cn.yuan.user.dto.LoginDto;
import cn.yuan.user.service.ILoginService;
import cn.yuan.util.AjaxResult.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = loginService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，稍后重试！！");
        }
    }
    /**
     * 微信登录
     * @param param
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> param){
        try {
            return loginService.wechatLogin(param.get("code"));

        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统错误");
        }
    }
    /**
     * 绑定微信用户
     * @param param
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult wechatBinder(@RequestBody Map<String, String> param){
        try {
            Map<String, Object> map = loginService.wechatBinder(param);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，稍后重试！！");
        }
    }
}
